/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExperimentsOverview.java
 *
 * Created on Jan 25, 2010, 5:02:35 PM
 */
package ch.epfl.dslab.conferrng.wizard;

import ch.epfl.dslab.conferrng.arugula.Configuration;
import ch.epfl.dslab.conferrng.arugula.ConfigurationWithError;
import ch.epfl.dslab.conferrng.engine.ConfigurationDiff;
import ch.epfl.dslab.conferrng.engine.FaultInjectionPlan;
import java.awt.Point;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import org.netbeans.spi.wizard.WizardPage;

/**
 *
 * @author slv
 */
public class ExperimentsOverview extends WizardPage {

    public static String getDescription() {
        return "Experiments overview";
    }

    public void renderingPage(){
        faultInjectionPlan.computeConfigurationWithErrors();
    }

    /** Creates new form ExperimentsOverview */
    public ExperimentsOverview() {
        initComponents();
    }
    private FaultInjectionPlan faultInjectionPlan;
    public static final String PROP_FAULTINJECTIONPLAN = "faultInjectionPlan";

    /**
     * Get the value of faultInjectionPlan
     *
     * @return the value of faultInjectionPlan
     */
    public FaultInjectionPlan getFaultInjectionPlan() {
        return faultInjectionPlan;
    }

    /**
     * Set the value of faultInjectionPlan
     *
     * @param faultInjectionPlan new value of faultInjectionPlan
     */
    public void setFaultInjectionPlan(FaultInjectionPlan faultInjectionPlan) {
        FaultInjectionPlan oldFaultInjectionPlan = this.faultInjectionPlan;
        this.faultInjectionPlan = faultInjectionPlan;
        propertyChangeSupport.firePropertyChange(PROP_FAULTINJECTIONPLAN, oldFaultInjectionPlan, faultInjectionPlan);
    }
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        originalConfigurationFile = new javax.swing.JEditorPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        modifiedConfigurationFile = new javax.swing.JEditorPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        differenceConfigurationFile = new javax.swing.JEditorPane();

        jLabel1.setText("These are the experiments that will be performed");

        jTable1.setModel(new  javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Type"
            }));
            jTable1.setColumnSelectionAllowed(true);
            jTable1.getTableHeader().setReorderingAllowed(false);

            org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${faultInjectionPlan.configurationErrors}");
            org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, this, eLProperty, jTable1);
            org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${description}"));
            columnBinding.setColumnName("Description");
            columnBinding.setColumnClass(String.class);
            columnBinding.setEditable(false);
            columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${shouldSucceed}"));
            columnBinding.setColumnName("Harmless");
            columnBinding.setColumnClass(Boolean.class);
            columnBinding.setEditable(false);
            bindingGroup.addBinding(jTableBinding);
            jTableBinding.bind();
            jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jTable1MouseClicked(evt);
                }
            });
            jScrollPane1.setViewportView(jTable1);
            jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

            jLabel2.setText("Original configuration file");

            jLabel3.setText("Modified configuration file");

            jLabel4.setText("Difference");

            originalConfigurationFile.setContentType("text/html");
            originalConfigurationFile.setEditable(false);
            jScrollPane2.setViewportView(originalConfigurationFile);

            modifiedConfigurationFile.setContentType("text/html");
            modifiedConfigurationFile.setEditable(false);
            jScrollPane3.setViewportView(modifiedConfigurationFile);

            differenceConfigurationFile.setContentType("text/html");
            differenceConfigurationFile.setEditable(false);
            jScrollPane4.setViewportView(differenceConfigurationFile);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 157, Short.MAX_VALUE))
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)))
                        .addComponent(jLabel1)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                        .addComponent(jLabel4))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane3)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                    .addGap(10, 10, 10)
                    .addComponent(jLabel4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                    .addContainerGap())
            );

            bindingGroup.bind();
        }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        Point p = evt.getPoint();
        modifiedConfigurationFile.setText("");
        originalConfigurationFile.setText("");
        int row = jTable1.rowAtPoint(p);
        ConfigurationWithError confWithErros = getFaultInjectionPlan().getConfigurationErrors().get(row);
        Configuration config = confWithErros.getConfiguration();
        String outputText = "<html>";
        String originalText = "";
        String modifiedText = "";
        for (String inputFile : config.getInputFileNameIterator()) {
            outputText += "<b>" + inputFile + "</b>";
            
            String modified = config.getModifiedConfigurationFile(inputFile);
            modifiedText += ("[" + inputFile + "]\n" + modified + "\n\n").replace("<", "&lt;").replace(">", "&gt;");

            String original = config.getContentsForInputFileName(inputFile);
            originalText += ("[" + inputFile + "]\n" + original + "\n\n").replace("<", "&lt;").replace(">", "&gt;");

            String val = ConfigurationDiff.getDiff(original, modified);

            val = val.replace("<", "&lt;").replace(">", "&gt;");
            if(val==null || val.equals("")){
                val = "There is not difference";
            }
            outputText += "<pre>" + val + "</pre>";
        }


        outputText += "</html>";
        modifiedConfigurationFile.setText(modifiedText);
        originalConfigurationFile.setText(originalText);
        differenceConfigurationFile.setText(outputText);

    }//GEN-LAST:event_jTable1MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane differenceConfigurationFile;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable jTable1;
    private javax.swing.JEditorPane modifiedConfigurationFile;
    private javax.swing.JEditorPane originalConfigurationFile;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
